package com.emex.app.stuff.admin;

import com.emex.app.shared.data.ProfitType;
import com.emex.app.stuff.gui.ProfitTypeDialog;
import com.emex.app.stuff.gui.model.ProfitTypeTableModel;
import com.emex.app.stuff.provider.ProfitDataProvider;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;
import org.openide.windows.WindowManager;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
        dtd = "-//com.emex.app.stuff.admin//ProfitType//EN",
        autostore = false
)
@TopComponent.Description(
        preferredID = "ProfitTypeTopComponent",
        //iconBase="SET/PATH/TO/ICON/HERE", 
        persistenceType = TopComponent.PERSISTENCE_NEVER
)
@TopComponent.Registration(mode = "editor", openAtStartup = false)
@ActionID(category = "Window", id = "com.emex.app.stuff.admin.ProfitTypeTopComponent")
@ActionReference(path = "Menu/Администрирование" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_ProfitTypeAction",
        preferredID = "ProfitTypeTopComponent"
)
@Messages({
    "CTL_ProfitTypeAction=Статьи доходов",
    "CTL_ProfitTypeTopComponent=Статьи доходов",
    "HINT_ProfitTypeTopComponent=Статьи доходов"
})
public final class ProfitTypeTopComponent extends TopComponent {

    private ProfitTypeTableModel profitTypeTableModel;

    public ProfitTypeTopComponent() {
        initComponents();
        setName(Bundle.CTL_ProfitTypeTopComponent());
        setToolTipText(Bundle.HINT_ProfitTypeTopComponent());
        customInit();
    }

    protected void customInit() {
        profitTypeTableModel = new ProfitTypeTableModel();
        profitTypeTable.setModel(profitTypeTableModel);
        profitTypeTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                int row = profitTypeTable.getSelectedRow();
                if ((row >= 0) && (row < profitTypeTable.getRowCount())) {
                    editProfitTypeButton.setEnabled(true);
                    deleteProfitTypeButton.setEnabled(true);

                } else {
                    editProfitTypeButton.setEnabled(false);
                    deleteProfitTypeButton.setEnabled(false);
                }
            }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonsPanel = new javax.swing.JPanel();
        editProfitTypeButton = new javax.swing.JButton();
        deleteProfitTypeButton = new javax.swing.JButton();
        addNewProfitTypeButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        profitTypeTable = new org.jdesktop.swingx.JXTable();

        buttonsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        org.openide.awt.Mnemonics.setLocalizedText(editProfitTypeButton, org.openide.util.NbBundle.getMessage(ProfitTypeTopComponent.class, "ProfitTypeTopComponent.editProfitTypeButton.text")); // NOI18N
        editProfitTypeButton.setEnabled(false);
        editProfitTypeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editProfitTypeButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(deleteProfitTypeButton, org.openide.util.NbBundle.getMessage(ProfitTypeTopComponent.class, "ProfitTypeTopComponent.deleteProfitTypeButton.text")); // NOI18N
        deleteProfitTypeButton.setEnabled(false);
        deleteProfitTypeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteProfitTypeButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(addNewProfitTypeButton, org.openide.util.NbBundle.getMessage(ProfitTypeTopComponent.class, "ProfitTypeTopComponent.addNewProfitTypeButton.text")); // NOI18N
        addNewProfitTypeButton.setToolTipText(org.openide.util.NbBundle.getMessage(ProfitTypeTopComponent.class, "ProfitTypeTopComponent.addNewProfitTypeButton.toolTipText")); // NOI18N
        addNewProfitTypeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addNewProfitTypeButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout buttonsPanelLayout = new javax.swing.GroupLayout(buttonsPanel);
        buttonsPanel.setLayout(buttonsPanelLayout);
        buttonsPanelLayout.setHorizontalGroup(
            buttonsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(buttonsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(addNewProfitTypeButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(editProfitTypeButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(deleteProfitTypeButton)
                .addContainerGap(79, Short.MAX_VALUE))
        );
        buttonsPanelLayout.setVerticalGroup(
            buttonsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, buttonsPanelLayout.createSequentialGroup()
                .addContainerGap(14, Short.MAX_VALUE)
                .addGroup(buttonsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addNewProfitTypeButton)
                    .addComponent(editProfitTypeButton)
                    .addComponent(deleteProfitTypeButton))
                .addContainerGap())
        );

        profitTypeTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                profitTypeTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(profitTypeTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addComponent(buttonsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(buttonsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void editProfitTypeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editProfitTypeButtonActionPerformed
        ProfitTypeDialog dialog = new ProfitTypeDialog(WindowManager.getDefault().getMainWindow(), true);
        dialog.setProfitType(getProfitTypeSelection());
        dialog.setProfitTypeTableModel(profitTypeTableModel);
        dialog.setLocationRelativeTo(null);
        dialog.setTitle("Редактировать статью дохода");
        dialog.setVisible(true);
    }//GEN-LAST:event_editProfitTypeButtonActionPerformed

    private void deleteProfitTypeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteProfitTypeButtonActionPerformed
        ProfitType selection = getProfitTypeSelection();
        ProfitDataProvider.getInstance().deleteProfitTypeByPrimaryKey(selection.getId());
        profitTypeTableModel.dataRemoved(selection);
    }//GEN-LAST:event_deleteProfitTypeButtonActionPerformed

    private void addNewProfitTypeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addNewProfitTypeButtonActionPerformed
        ProfitTypeDialog dialog = new ProfitTypeDialog(WindowManager.getDefault().getMainWindow(), true);
        dialog.setProfitTypeTableModel(profitTypeTableModel);
        dialog.setLocationRelativeTo(null);
        dialog.setTitle("Добавить новую статью доходов");
        dialog.setVisible(true);
    }//GEN-LAST:event_addNewProfitTypeButtonActionPerformed

    private void profitTypeTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_profitTypeTableMouseClicked
        if (evt.getClickCount() == 2) {
            editProfitTypeButtonActionPerformed(null);
        }
    }//GEN-LAST:event_profitTypeTableMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addNewProfitTypeButton;
    private javax.swing.JPanel buttonsPanel;
    private javax.swing.JButton deleteProfitTypeButton;
    private javax.swing.JButton editProfitTypeButton;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXTable profitTypeTable;
    // End of variables declaration//GEN-END:variables
    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    protected ProfitType getProfitTypeSelection() {
        if (profitTypeTable.getSelectedRow() > -1) {
            ProfitType profitType = (ProfitType) profitTypeTable
                    .getValueAt(profitTypeTable.getSelectedRow(), -1);
            return profitType;
        }
        return null;
    }
}
